package com.zhouyu.service;

import com.zhouyu.dto.AlipayTranDto;

import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

/**
 * @Description:文件导入reader,读取csv文件，使用FlatFileItemReader读取文件
 * springBatch给我们提供了很多reader,如FlatFileItemReader，JdbcCursorItemReader，JpaPagingItemReader等
 */
@Service
public class AlipayFileReader {

  private FlatFileItemReader<AlipayTranDto> flatFileItemReader;

  public AlipayFileReader() {
    flatFileItemReader = new FlatFileItemReader();
  }

  /**
   * 读取单个文件，然后映射为一对象 LineMapper的输入就是获取一行文本,有自定义MyLineMapper LineTokenizer:分割单行文本,然后包装成对象
   */
  public FlatFileItemReader<AlipayTranDto> readFile() {
    flatFileItemReader.setLineMapper(new DefaultLineMapper<AlipayTranDto>() {{
      setLineTokenizer(new DelimitedLineTokenizer() {{
        setNames(new String[]{"tranId", "channel", "tranType", "counterparty", "goods", "amount", "isDebitCredit", "state"});
      }});
      setFieldSetMapper(new BeanWrapperFieldSetMapper<AlipayTranDto>() {{
        setTargetType(AlipayTranDto.class);
      }});
    }});
    //设置忽略的行，第一行开始
    flatFileItemReader.setLinesToSkip(5);
    return flatFileItemReader;
  }

  /**
   * 读取多个文件 ResourceLoader:默认提供了classpath下的资源加载
   * PathMatchingResourcePatternResolver:提供了Ant风格的资源加载
   */
  public MultiResourceItemReader<AlipayTranDto> readFiles() {
    MultiResourceItemReader<AlipayTranDto> multiResourceReader = new MultiResourceItemReader<>();
    //获取多个文件位置
    /*ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver();
    Resource[] resources = new Resource[]{resourceLoader.getResource("/data/alipay/*.csv")};*/
    Resource[] resources = new Resource[]{new FileSystemResource("./data/alipay/208012345_20141030.csv"),
            new FileSystemResource("./data/alipay/208054321_20141030.csv")};
    multiResourceReader.setResources(resources);
    //委派给耽搁文件读取reader
    multiResourceReader.setDelegate(readFile());
    return multiResourceReader;
  }

  /**
   * 读取多个文件 ResourceLoader:默认提供了classpath下的资源加载
   * PathMatchingResourcePatternResolver:提供了Ant风格的资源加载
   */
  public MultiResourceItemReader<AlipayTranDto> readFilesByPath(String path) {
    MultiResourceItemReader<AlipayTranDto> multiResourceReader = new MultiResourceItemReader<>();
    //获取多个文件位置
    /*ResourceLoader resourceLoader = new PathMatchingResourcePatternResolver();
    Resource[] resources = new Resource[]{resourceLoader.getResource("/data/alipay/*.csv")};*/
    Resource[] resources = new Resource[]{new FileSystemResource(path)};
    multiResourceReader.setResources(resources);
    //委派给耽搁文件读取reader
    multiResourceReader.setDelegate(readFile());
    return multiResourceReader;
  }
}

